import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sns

pd.set_option("display.width",1000)
pd.set_option("display.max_rows",500)
pd.set_option("display.max_columns",500)

df=pd.read_csv('pima-indians-diabetes.csv')
print(df.head())

print("Train:",df.shape)

print(df.info())

print(df.describe())

NaN_col_names = ['Plasma_glucose_concentration','blood_pressure','Triceps_skin_fold_thickness','serum_insulin','BMI']
print((df[NaN_col_names] == 0).sum())

'''查看每个变量的分布极其与标签之间的关系'''
'''统计糖尿病不发病和发病的次数'''
sns.countplot(df['Target'])
plt.xlabel('Diabetes')
plt.ylabel('Number of occurrences')
plt.show()

'''怀孕次数'''
fig=plt.figure()
sns.countplot(df['pregnants'])
plt.xlabel('Number of pregnants')
plt.ylabel('Number of occurrences')
plt.show()
'''
ulimit=10
df=df[df['pregnants']<10]
print(df.shape)
'''
sns.countplot(x='pregnants',hue="Target",data=df)
plt.show()
'''随着怀孕次数的增加，糖尿病不发病数减少'''

'''血浆葡萄糖浓度'''
fig=plt.figure()
'''直方图'''
sns.distplot(df.Plasma_glucose_concentration,kde=False)
plt.xlabel('Plasma_glucose_concentration')
plt.ylabel('Number of occurrences')
plt.show()

sns.violinplot(x='Target',y='Plasma_glucose_concentration',data=df,hue='Target')
plt.xlabel('Diabetes',fontsize=12)
plt.ylabel('Plasma_glucose_concentration',fontsize=12)
plt.show()

'''血压'''

fig=plt.figure()
sns.distplot(df['blood_pressure'],kde=False)
plt.xlabel('blood_pressure')
plt.ylabel('frequency')
plt.show()

sns.violinplot(x='Target',y='blood_pressure',data=df,hue='Target')
plt.xlabel('Diabetes',fontsize=12)
plt.ylabel('blood_pressure',fontsize=12)
plt.show()

'''三头肌皮褶厚度'''
fig=plt.figure()
sns.distplot(df['Triceps_skin_fold_thickness'],kde=False)
plt.xlabel('Triceps_skin_fold_thickness')
plt.ylabel('frequency')
plt.show()

ulimit=80
df=df[df['Triceps_skin_fold_thickness']<80]
'''三头肌皮褶厚度取值分布'''
plt.scatter(range(df.shape[0]),df['Triceps_skin_fold_thickness'].values,color='purple')
plt.title('Distribution of Triceps_skin_fold_thickness')
plt.show()

sns.violinplot(x='Target',y='Triceps_skin_fold_thickness',data=df,hue='Target')
plt.xlabel('Diabetes',fontsize=12)
plt.ylabel('Triceps_skin_fold_thickness',fontsize=12)
plt.show()

'''餐后血清胰岛素'''
fig=plt.figure()
sns.distplot(df['serum_insulin'],kde=False)
plt.xlabel('serum_insulin')
plt.ylabel('frequency')
plt.show()

sns.violinplot(x='Target',y='serum_insulin',data=df,hue='Target')
plt.xlabel('Diabetes',fontsize=12)
plt.ylabel('serum_insulin',fontsize=12)
plt.show()

'''体重指数'''
fig=plt.figure()
sns.distplot(df['BMI'],kde=False)
plt.xlabel('BMI')
plt.ylabel('frequency')
plt.show()

sns.violinplot(x='Target',y='BMI',data=df,hue='Target')
plt.xlabel('Diabetes',fontsize=12)
plt.ylabel('BMI',fontsize=12)
plt.show()
'''BMI为0，为缺失值'''
BMIDF=df.groupby(['BMI','Target'])['BMI'].count().unstack('Target').fillna(0)
BMIDF[[0,1]].plot(kind='bar',stacked=True)
plt.show()

'''糖尿病家系作用'''
fig=plt.figure()
sns.distplot(df['Diabetes_pedigree_function'],kde=False)
plt.xlabel('Diabetes_pedigree_function')
plt.ylabel('frequency')
plt.show()

DF = df.groupby(['Diabetes_pedigree_function', 'Target'])['Diabetes_pedigree_function'].count().unstack('Target').fillna(0)
DF[[0,1]].plot(kind='bar', stacked=True)
plt.show()

'''年龄'''
fig=plt.figure()
sns.distplot(df['Age'],kde=False)
plt.xlabel('Age')
plt.ylabel('frequency')
plt.show()

DF = df.groupby(['Age', 'Target'])['Age'].count().unstack('Target').fillna(0)
DF[[0,1]].plot(kind='bar', stacked=True)
plt.show()

'''特征之间的关系'''
data_corr=df.corr().abs()
plt.subplots(figsize=(13,9))
sns.heatmap(data_corr,annot=True)
plt.show()

for feature in df.columns:
    sns.distplot(df[feature],kde=False)
    plt.show()